using UnityEngine;

public class JikuCamera : MonoBehaviour
{
	public Transform m_trans;

	public float speedx;

	public float speedy;

	public float rotationX;

	public float rotationY;

	private float preRotationX;

	private float preRotationY;

	private float sensitivityX = 0.5f;

	private float sensitivityY = 0.5f;

	public float lerpRotationX;

	public float lerpRotationY;

	public bool flag;

	public bool lockCamera;

	public float lerpPercent;

	//public Animation startanima;
	public Animator animator;

	public Transform postra;

	private void Awake()
	{
		lockCamera = true;
	}

	public void startfun()
	{
		/* startanima["showplane"].time = 0f;
		startanima["showplane"].speed = 1f;
		startanima.Play(); */
		animator.SetTrigger("aircraftsPopOpen");
		Invoke("startset", animator.runtimeAnimatorController.animationClips[0].length);
	}

	public void startset()
	{
		lockCamera = false;
		Vector3 localEulerAngles = m_trans.transform.localEulerAngles;
		rotationX = localEulerAngles.y;
		Vector3 localEulerAngles2 = m_trans.transform.localEulerAngles;
		rotationY = localEulerAngles2.x;
		if (rotationY > 180f)
		{
			rotationY = 360f - rotationY;
		}
	}

	Vector3 lastPos = Vector3.zero;
	private void Update()
	{
		if (lockCamera)
		{
			postra.Rotate(0f, -5f * Time.deltaTime, 0f, Space.World);
			if (!(lerpPercent >= 1f))
			{
				rotationY = Mathf.Lerp(lerpRotationY, 0f, lerpPercent);
				lerpPercent += Time.deltaTime * 1.5f;
				if (lerpPercent >= 1f)
				{
					lerpPercent = 1f;
				}
				m_trans.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
				m_trans.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
			}
			return;
		}
		rotationX %= 360f;
		if (UnityEngine.Input.touchCount > 0 && UnityEngine.Input.GetTouch(0).phase == TouchPhase.Moved)
		{
			preRotationX = rotationX;
			preRotationY = rotationY;
			if (UnityEngine.Input.GetTouch(0).deltaPosition.magnitude < 100f)
			{
				float num = rotationX;
				Vector2 deltaPosition = UnityEngine.Input.GetTouch(0).deltaPosition;
				rotationX = num + deltaPosition.x * sensitivityX;
				float num2 = rotationY;
				Vector2 deltaPosition2 = UnityEngine.Input.GetTouch(0).deltaPosition;
				rotationY = num2 + deltaPosition2.y * sensitivityY;
			}
			speedx = rotationX - preRotationX;
			speedy = rotationY - preRotationY;
			flag = false;
		}
		if (Input.touchCount == 0)
		{
			if (UnityEngine.Input.GetMouseButtonDown(0))
			{
				lastPos = UnityEngine.Input.mousePosition;
			}
			if (UnityEngine.Input.GetMouseButton(0) && !UnityEngine.Input.GetMouseButtonDown(0))
			{
				preRotationX = rotationX;
				preRotationY = rotationY;
				Vector3 pos = UnityEngine.Input.mousePosition - lastPos;
				if (pos.magnitude < 1000f)
				{
					float num = rotationX;
					Vector2 deltaPosition = pos;
					rotationX = num + deltaPosition.x * sensitivityX;
					float num2 = rotationY;
					Vector2 deltaPosition2 = pos;
					rotationY = num2 + deltaPosition2.y * sensitivityY;
				}
				speedx = rotationX - preRotationX;
				speedy = rotationY - preRotationY;
				flag = false;
				lastPos = UnityEngine.Input.mousePosition;
			}

			if (UnityEngine.Input.GetMouseButtonUp(0))
			{
				flag = true;
			}
		}
		if (UnityEngine.Input.touchCount > 0 && UnityEngine.Input.GetTouch(0).phase == TouchPhase.Ended)
		{
			flag = true;
		}
		if (flag)
		{
			rotationX += speedx;
			rotationY += speedy;
			speedx -= speedx / 20f;
			speedy -= speedy / 20f;
		}
		doRotate();
	}

	private void doRotate()
	{
		if (rotationY < -30f)
		{
			rotationY = -30f;
		}
		else if (rotationY > 10f)
		{
			rotationY = 10f;
		}
		m_trans.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
		m_trans.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
	}

	public void ResetRotate()
	{
		lockCamera = true;
		lerpRotationX = rotationX;
		lerpRotationY = rotationY;
		lerpPercent = 0f;
		speedx = 0f;
		speedy = 0f;
		endanima();
	}

	public void endanima()
	{
		/* startanima["showplane"].time = startanima.clip.length;
		startanima["showplane"].speed = -1f;
		startanima.Play(); */
		animator.SetTrigger("aircraftsPopClose");
	}
}
